package com.tansun.easycare.modules.sys.entity;
/**
 * MyRequestWrapper.java
 * 
 * @screen
 * @author havery
 */

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import org.apache.commons.io.IOUtils;

public class XssSqlHttpServletRequestWrapper extends HttpServletRequestWrapper
{
        HttpServletRequest orgRequest = null;
        private byte[] bytes;
        private WrappedServletInputStream  wrappedServletInputStream;

        public XssSqlHttpServletRequestWrapper(HttpServletRequest request) throws IOException {
          super(request);
          this.orgRequest = request;
          //读取输入流的请求参数，保存到bytes中
          bytes = IOUtils.toByteArray(request.getInputStream());
          ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
          this.wrappedServletInputStream = new WrappedServletInputStream(byteArrayInputStream);

          //把post参数重新写入请求流
          reWriteInputStream();
  }

  /**
   * 把参数重新写进请求里
   */
   public void reWriteInputStream() {
      wrappedServletInputStream.setStream(new ByteArrayInputStream(bytes != null ? bytes : new byte[0]));
  }

   @Override
   public ServletInputStream getInputStream() throws IOException {
       return wrappedServletInputStream;
   }

   @Override
   public BufferedReader getReader() throws IOException {
       return new BufferedReader(new InputStreamReader(wrappedServletInputStream));
   }

   /**
    * 获取post参数
    */
   public String getRequestParams() throws IOException {
       return new String(bytes, this.getCharacterEncoding());
   }

  //清洗参数，防止xss注入
  public String[] getParameterValues(String parameter)
  {
          String[] values = super.getParameterValues(parameter);
          if (values == null) {
            return null;
    }
          int count = values.length;
          String[] encodedValues = new String[count];
          for (int i = 0; i < count; i++) {
            encodedValues[i] = xssEncode(values[i]);
    }
          return encodedValues;
  }


  public String getParameter(String name)
  {
          String value = super.getParameter(xssEncode(name));
          if (value != null) {
            value = xssEncode(value);
    }
          return value;
  }

  public String getHeader(String name)
  {
          String value = super.getHeader(xssEncode(name));
          if (value != null) {
            value = xssEncode(value);
    }
          return value;
  }


  private static String xssEncode(String s)
  {
//          return StringUtil.xssEncode(s);
	  return s;
  }





  public HttpServletRequest getOrgRequest()
  {
          return this.orgRequest;
  }


  public static HttpServletRequest getOrgRequest(HttpServletRequest req)
  {
          if ((req instanceof XssSqlHttpServletRequestWrapper)) {
            return ((XssSqlHttpServletRequestWrapper)req).getOrgRequest();
    }

        return req;
  }


  private class WrappedServletInputStream extends ServletInputStream {
      public void setStream(InputStream stream) {
          this.stream = stream;
      }
      private InputStream stream;
      public WrappedServletInputStream(InputStream stream) {
          this.stream = stream;
      }
      public int read() throws IOException {
          return stream.read();
      }

      public boolean isFinished() {
          return true;
      }

      public boolean isReady() {
          return true;
      }
	@Override
	public void setReadListener(ReadListener listener) {
		// TODO Auto-generated method stub
		
	}
	
  }

}